package com.china08.yxyapi.api.home.kecheng.cloud;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsVideo;
import com.china08.yxyapi.entity.db.lessons.cloud.YxyLessonsCloudLesson;
import com.china08.yxyapi.entity.db.lessons.cloud.YxyLessonsCloudLessonItem;
import com.china08.yxyapi.entity.mg.lessons.YxyLessonsUserScore;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyLessonsVideoRepository;
import com.china08.yxyapi.repository.db.lesson.YxyLessonsCloudLessonItemRepository;
import com.china08.yxyapi.repository.db.lesson.YxyLessonsCloudLessonRepository;
import com.china08.yxyapi.repository.mg.YxyLessonsUserScoreRepository;

/**
 * 
 * @annotation: 云课程--观看课程详情
 * @author: yaoguanglin
 */
@CrossOrigin
@RestController
public class KechengCloudLessonViewController extends YxyApiBaseController {

    @Autowired
    private YxyLessonsCloudLessonRepository yxyLessonsCloudLessonRepository;

    @Autowired
    private YxyLessonsCloudLessonItemRepository yxyLessonsCloudLessonItemRepository;

    @Autowired
    private YxyLessonsUserScoreRepository yxyLessonsUserScoreRepository;

    @Autowired
    private YxyLessonsVideoRepository yxyLessonsVideoRepository;

    @RequestMapping(value = "/api_yw/home/kecheng/cloud/lesson/view/srh", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess(HttpServletRequest request, HttpServletResponse response,
            @RequestBody(required = false) ReqModel reqModel) {

        String lessonId = reqModel.lessonId;
        String userId = getProfile(request, response).getId();
        String domainName = getDomainName(request);
        YxyLessonsCloudLesson cloudLesson = yxyLessonsCloudLessonRepository.findById(lessonId);
        List<LessonItem> lessonItems = new ArrayList<KechengCloudLessonViewController.LessonItem>();
        List<YxyLessonsCloudLessonItem> list = yxyLessonsCloudLessonItemRepository
                .findByLessonIdAndStatusOrderByOrderNum(lessonId, 2);
        RespModel respModel = new RespModel();
        int i = 1;
        for (YxyLessonsCloudLessonItem yxyLessonsCloudLessonItem : list) {
            LessonItem item = new LessonItem();
            // 获取视频时长
            YxyLessonsVideo yxyLessonsVideo = yxyLessonsVideoRepository
                    .findById(yxyLessonsCloudLessonItem.getVideoId());
            item.duration = yxyLessonsVideo == null ? 0 : yxyLessonsVideo.getDuration();
            item.lessonId = yxyLessonsCloudLessonItem.getLessonId();
            item.lessonItemId = yxyLessonsCloudLessonItem.getId();
            item.lessonItemIntro = yxyLessonsCloudLessonItem.getIntro();
            item.lessonItemTitle = yxyLessonsCloudLessonItem.getTitle();
            item.status = yxyLessonsVideo.getStatus();
            item.orderNum = i;
            String uuid = UUID.randomUUID().toString().replace("-", "");
            String md5 = yxyLessonsVideo.getMd5();
            String url = "";
            if (domainName.contains("hrbedu")) {
                url = "https://yxyres.hrbeduy.com/converted/mp4/";
            } else if (domainName.contains("91yunxiao")) {
                url = "https://yxy.res.91yunxiao.com/converted/mp4/";
            } else {
                url = "https://yxy.res.91yunxiao.com/converted/mp4/";
            }
            item.playUrl = url + md5.substring(0, 3) + "/" + md5.substring(3, 6) + "/" + md5
                    + ".mp4?&lessonsCloud_sign=" + uuid;
            lessonItems.add(item);
            i++;
        }

        YxyLessonsUserScore lessonsUserScore = yxyLessonsUserScoreRepository.findByUserIdAndLessonsId(userId, lessonId);
        if (lessonsUserScore != null) {
            respModel.rate = lessonsUserScore.getScore();
            respModel.mark = 1;
        } else {
            respModel.mark = 0;
            respModel.rate = 0;
        }

        respModel.lessonItems = lessonItems;
        respModel.cover = cloudLesson.getCover();
        respModel.name = cloudLesson.getName();
        respModel.markNum = cloudLesson.getEpTimes();
        return respModel;
    }

    protected static class ReqModel extends YxyApiReqModel {
        // 课程ID
        public String lessonId;
    }

    protected static class RespModel extends YxyApiRespModel {
        // 课程名称
        public String name;
        // 是否已经评价0 没有 1 已评价
        public Integer mark;
        // 评价人数
        public Integer markNum;
        // 评分
        public float rate;
        // 课时信息
        public List<LessonItem> lessonItems;
        // 课程封面
        public String cover;
    }

    /** 课时 */
    protected static class LessonItem {
        // 课时名称
        public String lessonItemTitle;
        // 课时简介
        public String lessonItemIntro;
        // 课程id
        public String lessonId;
        // 课时Id
        public String lessonItemId;
        // 视频时长
        public Integer duration;
        // 序号
        public Integer orderNum;
        // 播放地址
        public String playUrl;
        // 课时状态 是否在转码
        public Integer status;
    }
}
